/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.oki;

import java.lang.reflect.InvocationTargetException;
import org.ontspace.oki.owl.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ieee.ltsc.lom.LOM;
import org.osid.repository.Asset;
import org.osid.repository.AssetIterator;
import org.osid.repository.RecordStructureIterator;
import org.osid.repository.Repository;
import org.osid.repository.RepositoryException;
import org.osid.shared.Id;
import org.osid.shared.LongValueIterator;
import org.osid.shared.Properties;
import org.osid.shared.PropertiesIterator;
import org.osid.shared.SharedException;
import org.osid.shared.Type;
import org.osid.shared.TypeIterator;
import org.ontspace.MetadataRepository;
import org.ontspace.lom.LOMQueryManager;

import org.ontspace.comp.COMPQueryManager;
import org.ontspace.comp.owl.COMPQueryResultImpl;
import org.ontspace.dc.DCQueryManager;
import org.ontspace.dc.owl.DCQueryResultImpl;
import org.ontspace.lom.owl.LOMQueryResultImpl;
import org.ontspace.owl.QueryResultImpl;
import org.ontspace.owl.util.OntologyNotInRepositoryException;
import org.ontspace.sld.SLDQueryManager;
import org.ontspace.sld.owl.SLDQueryResultImpl;

/**
 * Creates a new instance of OKIRepository
 */
public class OntspaceOKIRepositoryImpl implements Repository {

    /** The ont-space repository*/
    MetadataRepository _rep =
            null;

    /**
     * Creates the OspaceOKIRepository
     * @param rep The MetadataRepository
     */
    OntspaceOKIRepositoryImpl(MetadataRepository rep) {
        _rep =
                rep;
    }

    public void updateDisplayName(String arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported.");
    }

    /**
     * Gets the repository name
     * @return repository name
     * @throws org.osid.repository.RepositoryException
     */
    public String getDisplayName() throws RepositoryException {
        return "ont-space repository at " +
                _rep.getRepositoryURI().toString();
    }

    /**
     * Get the ont-space oki Id
     * @return  a new instance of OntspaceOKIId
     * @throws org.osid.repository.RepositoryException
     */
    public Id getId() throws RepositoryException {
        return new OntspaceOKIId(_rep.getRepositoryURI());
    }

    /**
     *   Perform a search of the specified Type and get all the Assets
     * that satisfy the SearchCriteria.
     * @param arg0 the serializable object
     * @param type searchType
     * @param arg2 properties to set the search criteria
     * @return the aset iterator with the results
     */
    public AssetIterator getAssetsBySearch(Serializable arg0, Type type,
            Properties arg2) {

        AssetIterator results =
                null;
        QueryResultImpl qResults =
                new QueryResultImpl();
        String queryMethod =
                type.getDomain();


        try {



            String repositoryType =
                    type.getKeyword();
            //repositoryType: lom ||sld||dc

            if (repositoryType.compareTo("http://www.cc.uah.es/ie/ont/lom-ospace") ==
                    0) {

                HashMap paramsLOM =
                        new HashMap();
                paramsLOM.put("uri1", "http://www.cc.uah.es/ie/ont/lom-ospace");


                //Get the queryManager
                LOMQueryManager qm =
                        (LOMQueryManager) _rep.getQueryManager("org.ontspace.lom.owl.LOMQueryManagerImpl",
                        paramsLOM);


                //configure the query params
                LOM lom =
                        null;
                //HashMap params = null;
                List cm =
                        null;

                //Get the original lom
                OntspaceOKIOriginalLom Olom =
                        new OntspaceOKIOriginalLom((OspaceOKISerializedLomImpl) arg0);

                try {
                    lom =
                            Olom.BuildLom();
                } catch (java.text.ParseException e) {
                    System.out.print(e.getMessage());
                }
                try {
                    List properties =
                            (List) arg2.getProperty(null);

                    cm =
                            (List) properties.get(0);
                } catch (SharedException ex) {
                    System.out.print(ex.getMessage());
                }
                LOMQueryResultImpl r =
                        new LOMQueryResultImpl();

                ////                
                Class[] parameters =
                        new Class[2];
                Object[] parameter =
                        new Object[2];
                parameters[0] =
                        LOM.class;
                parameters[1] =
                        List.class;
                parameter[0] =
                        lom;
                parameter[1] =
                        cm;
                Method query;
                query =
                        qm.getClass().getMethod(queryMethod, parameters);
                r =
                        (LOMQueryResultImpl) query.invoke(qm, parameter);
                /////



                qResults =
                        r;
                results =
                        new OspaceOKIAssetIteratorImpl(qResults);
            } else if (repositoryType.compareTo("http://www.cc.uah.es/ie/ont/sld-ospace") ==
                    0) {

                HashMap paramsSLD =
                        new HashMap();
                paramsSLD.put("uri1", "http://www.cc.uah.es/ie/ont/sld-ospace");


                //Get the queryManager
                SLDQueryManager qm =
                        (SLDQueryManager) _rep.getQueryManager("org.ontspace.sld.owl.SLDQueryManagerImpl",
                        paramsSLD);
                SLDQueryResultImpl r =
                        new SLDQueryResultImpl();

                ////                                   
                Method query;
                query =
                        qm.getClass().getMethod(queryMethod);
                r =
                        (SLDQueryResultImpl) query.invoke(qm);
                /////


                qResults =
                        r;
                results =
                        new OspaceOKIAssetIteratorImpl(qResults);
            } else if (repositoryType.compareTo("http://www.cc.uah.es/ie/ont/dc-ospace") ==
                    0) {

                HashMap paramsDC =
                        new HashMap();
                paramsDC.put("uri1", "http://www.cc.uah.es/ie/ont/dc-ospace");

                //Get the queryManager
                DCQueryManager qm =
                        (DCQueryManager) _rep.getQueryManager("org.ontspace.dc.owl.DCQueryManagerImpl",
                        paramsDC);
                DCQueryResultImpl r =
                        new DCQueryResultImpl();
                ////                
                Class[] parameters =
                        new Class[1];
                Object[] parameter =
                        new Object[1];
                parameters[0] =
                        String.class;
                parameter[0] =
                        arg0;
                Method query;
                query =
                        qm.getClass().getMethod(queryMethod, parameters);
                r =
                        (DCQueryResultImpl) query.invoke(qm, parameter);

                qResults =
                        r;
                results =
                        new OspaceOKIAssetIteratorImpl(qResults);
            } else if (repositoryType.compareTo("http://www.cc.uah.es/ie/ont/cLom-ospace") ==
                    0) {

                HashMap paramsCOMP =
                        new HashMap();
                paramsCOMP.put("uri1", "http://www.cc.uah.es/ie/ont/cLom-ospace");


                //Get the queryManager
                COMPQueryManager qm =
                        (COMPQueryManager) _rep.getQueryManager("org.ontspace.comp.owl.COMPQueryManagerImpl",
                        paramsCOMP);

                //configure the query params                                                            
                COMPQueryResultImpl r =
                        new COMPQueryResultImpl();
                Class[] parameters =
                        new Class[1];
                Object[] parameter =
                        new Object[1];
                parameters[0] =
                        List.class;
                parameter[0] =
                        arg0;
                Method query;
                query =
                        qm.getClass().getMethod(queryMethod, parameters);
                r =
                        (COMPQueryResultImpl) query.invoke(qm, parameter);

                qResults =
                        r;
                results =
                        new OspaceOKIAssetIteratorImpl(qResults);
            }









        } catch (IllegalAccessException ex) {
            Logger.getLogger(OntspaceOKIRepositoryImpl.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(OntspaceOKIRepositoryImpl.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(OntspaceOKIRepositoryImpl.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(OntspaceOKIRepositoryImpl.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(OntspaceOKIRepositoryImpl.class.getName()).log(Level.SEVERE,
                    null, ex);
        } catch (OntologyNotInRepositoryException ex) {
            Logger.getLogger(OntspaceOKIRepositoryImpl.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return results;
    }

    public Type getType() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getDescription() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void updateDescription(String arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Asset createAsset(String arg0, String arg1, Type arg2) 
            throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void deleteAsset(Id arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public AssetIterator getAssets() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public AssetIterator getAssetsByType(Type arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TypeIterator getAssetTypes() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Properties getPropertiesByType(Type arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TypeIterator getPropertyTypes() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public PropertiesIterator getProperties() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public RecordStructureIterator getRecordStructures() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public RecordStructureIterator getRecordStructuresByType(Type arg0)
            throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public RecordStructureIterator getMandatoryRecordStructures(Type arg0)
            throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TypeIterator getSearchTypes() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public TypeIterator getStatusTypes() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Type getStatus(Id arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean validateAsset(Id arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void invalidateAsset(Id arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Asset getAsset(Id arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Asset getAssetByDate(Id arg0, long arg1) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public LongValueIterator getAssetDates(Id arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Id copyAsset(Asset arg0) throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean supportsVersioning() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean supportsUpdate() throws RepositoryException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
